#!/bin/bash

# 将脚本运行中过程生成的文件集中存放
BASE_DIR=$(pwd)
TEMP_DIR=$BASE_DIR/init_ubuntu
if [ ! -d $TEMP_DIR ];then
    mkdir -p $TEMP_DIR
fi

repeat() {
    while :
    do
        $@ && return
        sleep 5
    done
}

command_exists() {
	command -v "$@" > /dev/null 2>&1
}

user="$(id -un 2>/dev/null || true)"

sh_c='sh -c'
if [ "$user" != 'root' ]; then
    if command_exists sudo; then
        sh_c='sudo -E sh -c'
    elif command_exists su; then
        sh_c='su -c'
    else
        echo "Error: this installer needs the ability to run commands as root.We are unable to find either "sudo" or "su" available to make this happen."
        exit 1
    fi
fi

# 系统更新地址切换到国内
step1_update_mirrors() {
    read -p "是否需要切换镜像源？(0:不切换; 1:切换)" is_update_mirrors
    if [ $is_update_mirrors -eq 1 ];then
        echo "1 => 系统更新地址切换到国内......"
        source_address=$(cat /etc/apt/sources.list | grep "deb " | awk 'NR==1{print}' | awk -F " " '{print $2}')
        echo $source_address
        if [ $source_address == "http://archive.ubuntu.com/ubuntu/" ];then
            mirrors_type=-1
            while [ $mirrors_type -eq -1 ];do
                read -p"选择将系统更新地址切换到国内哪个镜像源:1. 清华源;2. 阿里源;3. 华为源;4. 科大源;5. 不切换。" mirrors_type
                if [ $mirrors_type -eq 1 ];then
                    # 清华源
                    $sh_c 'sed -e "s|http://archive.ubuntu.com|http://mirrors.tuna.tsinghua.edu.cn|g"\
                        -e "s|http://security.ubuntu.com|http://mirrors.tuna.tsinghua.edu.cn|g"\
                        -i.bak /etc/apt/sources.list'
                elif [ $mirrors_type -eq 2 ];then
                    # 阿里源
                    $sh_c 'sed -e "s|http://archive.ubuntu.com|http://mirrors.aliyun.com|g"\
                        -e "s|http://security.ubuntu.com|http://mirrors.aliyun.com|g"\
                        -i.bak /etc/apt/sources.list'
                elif [ $mirrors_type -eq 3 ];then
                    # 华为源
                    $sh_c 'sed -e "s|http://archive.ubuntu.com|http://repo.huaweicloud.com|g"\
                        -e "s|http://security.ubuntu.com|http://repo.huaweicloud.com|g"\
                        -i.bak /etc/apt/sources.list'
                elif [ $mirrors_type -eq 4 ];then
                    # 科大源
                    $sh_c 'sed -e "s|http://archive.ubuntu.com|http://mirrors.ustc.edu.cn|g"\
                        -e "s|http://security.ubuntu.com|http://mirrors.ustc.edu.cn/|g"\
                        -i.bak /etc/apt/sources.list'
                elif [ $mirrors_type -eq 5 ];then
                    # 不切换
                    let mirrors_type=5
                else
                    let mirrors_type=-1
                fi
            done
            $sh_c 'apt-get update'
        fi
        echo "1 => 系统更新地址切换到国内......OK"
    fi
}

# ssh 本机免密
step2_set_ssh_quicklogin() {
    if [ -d $BASE_DIR/.ssh ] && [ -f $BASE_DIR/.ssh/id_rsa ] && [ -f $BASE_DIR/.ssh/id_rsa.pub ];then
        echo "2 => ssh 本机登录免密......"
        if [ ! -d ~/.ssh ];then
            mkdir -p ~/.ssh/
        fi
        cp $BASE_DIR/.ssh/id_rsa* ~/.ssh/
        if [ ! -f /usr/sbin/ifconfig ];then
            $sh_c 'apt-get install -y net-tools'
        fi
        read -p "请输入需要 ssh 免密登录的 IP 地址:" ip
        ssh-copy-id -i ~/.ssh/id_rsa.pub "$ip"
        echo "2 => ssh 本机登录免密......OK"
    else
        echo "请将可免密登录 ssh 私钥复制到 $BASE_DIR/.ssh 目录下!"
    fi
}

# zsh
step3_install_zsh() {
    read -p "是否需要安装zsh？(0:不安装；1：安装)" is_install_zsh
    if [ $is_install_zsh -eq 1 ];then
        echo "3 => 安装 zsh......"
        if [ ! -d ~/.oh-my-zsh ];then
            $sh_c 'apt-get install -y zsh git curl wget'
            
            # 报错: server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
            # git config --global http.sslverify false
            cd $BASE_DIR && sh install_zsh.sh
            #sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
        fi
        cp yhw-miracle.zsh-theme ~/.oh-my-zsh/themes/
        cp ~/.zshrc ~/.zshrc.copy && cp .zshrc ~/

        if [ ! -d $TEMP_DIR/fonts ];then
            cd $TEMP_DIR
            git clone https://gitee.com/yhw-miracle/fonts.git && cd fonts && sh install.sh
            #git clone https://github.com/powerline/fonts.git && cd fonts && sh install.sh
        fi
        echo "3 => 安装 zsh......OK"
    fi
}

# vim

step4_install_and_config_vim() {
    read -p "是否需要安装vim？(0:不安装；1：安装)" is_install_vim
    if [ $is_install_vim -eq 1 ];then
        $sh_c 'apt-get install -y vim'
        $sh_c 'apt-get install -y tmux'
    fi
    read -p "是否需要配置vim？(0:不安装；1：安装)" is_config_vim
    if [ $is_config_vim -eq 1 ];then
        echo "4 => 安装并配置 vim......"
        cd $TEMP_DIR
        #if [ ! -d $TEMP_DIR/config_vim ];then
        #    # 有可能会下载失败
        #    git clone https://gitee.com/yhw-miracle/config_vim.git --recursive && cd config_vim
        #else
        #    cd config_vim && git pull && git submodule init && git submodule update
        #fi
        cd $BASE_DIR/config_vim
        cp -r .vim ~/
        cp -r vimcolorschemes/* ~/.vim/plugged && cp -r vimplugins/* ~/.vim/plugged
        cp ubuntu/.vimrc ~/
        echo "4 => 安装并配置 vim......OK"
    fi
}

# docker
step5_install_and_config_docker() {
    read -p "是否安装 docker(0:不安装; 1:安装):" is_install_docker
    if [ ! -f /usr/bin/docker ] && [ $is_install_docker -eq 1 ];then
        echo "5 => 安装并配置 docker......"
        cd $TEMP_DIR
        curl -fsSL https://get.docker.com -o get-docker.sh
        #public key containerd.io.1.5.11-3.1.el7.x86_64.rpm is not installed
        #cd $TEMP_DIR
        #wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.5.11-3.1.el7.x86_64.rpm && yum install -y containerd.io-1.5.11-3.1.el7.x86_64.rpm
        #wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-cli-20.10.14-3.el7.x86_64.rpm && yum install -y docker-ce-cli-20.10.14-3.el7.x86_64.rpm
        #wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.14-3.el7.x86_64.rpm && yum install -y docker-ce-20.10.14-3.el7.x86_64.rpm
        sh get-docker.sh
    fi

    #if [ -f /usr/bin/docker ];then
    if command_exists docker; then
        docker_config_dir=/etc/docker
        if [ ! -d $docker_config_dir ];then
            $sh_c 'mkdir -p '$docker_config_dir''
        fi

        docker_config_file=$docker_config_dir/daemon.json
        if [ ! -f $docker_config_file ];then
            $sh_c 'touch '$docker_config_file''
        fi

        docker_home_dir=/data/docker_home
        if [ ! -d $docker_home_dir ];then
            $sh_c 'mkdir -p '$docker_home_dir''
        fi
        
        $sh_c 'echo "{" > '$docker_config_file''
        $sh_c 'echo "    \"registry-mirrors\": [\"https://bpt6ffci.mirror.aliyuncs.com\"]," >> '$docker_config_file''
        $sh_c 'echo "    \"data-root\": \"/data/docker_home\"" >> '$docker_config_file''
        $sh_c 'echo "}" >> '$docker_config_file''
        docker_status=$(systemctl status docker.service | grep Active | awk -F " " '{print $3}')
        if [ "$docker_status" = "(running)" ];then
            $sh_c 'systemctl restart docker.service'
        elif [ "$docker_status" = "(dead)" ];then
            $sh_c 'systemctl start docker.service'
        fi
        
        # docker-compose
        $sh_c 'cp '$BASE_DIR'/docker/docker-compose /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose'

        echo "5 => 安装并配置 docker......OK"
    fi
}

step1_update_mirrors
step2_set_ssh_quicklogin
step3_install_zsh
step4_install_and_config_vim
step5_install_and_config_docker

